package com.sendtion.xrichtext;

import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.Image;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.app.Context;
import ohos.media.image.PixelMap;

/**
 * 自定义ImageView，可以存放PixelMap和Path等信息
 *
 * @since 2021-06-22
 */
public class DataImageView extends Image implements Component.DrawTask {
    private static final int INT = 5;
    private boolean isShowBorder = false;
    private Color borderColor = Color.GRAY;
    private int borderWidth = INT;

    private String absolutePath;
    private PixelMap bitmap;
    private Paint paint;

    /**
     * DataImageView
     *
     * @param context
     */
    public DataImageView(Context context) {
        super(context);
    }

    /**
     * DataImageView
     *
     * @param context
     * @param attrSet
     */
    public DataImageView(Context context, AttrSet attrSet) {
        super(context, attrSet);
    }

    /**
     * DataImageView
     *
     * @param context
     * @param attrSet
     * @param styleName
     */
    public DataImageView(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        initData();
    }

    private void initData() {
        paint = new Paint();
        paint.setColor(borderColor);
        paint.setStrokeWidth(borderWidth);
        paint.setStyle(Paint.Style.STROKE_STYLE);
        addDrawTask(this::onDraw);
    }

    public String getAbsolutePath() {
        return absolutePath;
    }

    public void setAbsolutePath(String absolutePath) {
        this.absolutePath = absolutePath;
    }

    public boolean isShowBorder() {
        return isShowBorder;
    }

    public void setShowBorder(boolean isShowBorder) {
        this.isShowBorder = isShowBorder;
    }

    public PixelMap getBitmap() {
        return bitmap;
    }

    public void setBitmap(PixelMap bitmap) {
        this.bitmap = bitmap;
    }

    public Color getBorderColor() {
        return borderColor;
    }

    public void setBorderColor(Color borderColor) {
        this.borderColor = borderColor;
    }

    public int getBorderWidth() {
        return borderWidth;
    }

    public void setBorderWidth(int borderWidth) {
        this.borderWidth = borderWidth;
    }

    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (isShowBorder) {
            Rect rec = canvas.getDeviceClipBounds();
            canvas.drawRect(rec, paint);
        }
    }
}
